import winston from "winston";
import path from "path";
import {fileURLToPath} from "url";

// 转换为文件系统路径
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const logger = winston.createLogger({
 level: "info",
 format: winston.format.combine(
  winston.format.timestamp(),
  winston.format.printf((res) => {
   const {timestamp, level, message} = res;
   if (level === "error") {
    return `${timestamp} ${level}: ${JSON.stringify(res)}`;
   }
   return `${timestamp} ${level}: ${message}`;
  })
 ),
 transports: [
  new winston.transports.File({
   filename: path.resolve(__dirname, "../logs/cli.log"),
  }),
 ],
});

export default logger;
